package com.karl.loadingviews.headers;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.karl.loading.loadings.AliPayLoadingView;
import com.karl.loadingviews.BaseHeaderView;
import com.karl.loadingviews.R;
import com.lcodecore.tkrefreshlayout.IHeaderView;
import com.lcodecore.tkrefreshlayout.OnAnimEndListener;

/**
 * Created by CoderFan on 2017/8/26.
 *
 */

public class AliPayHeaderView extends BaseHeaderView {

    private AliPayLoadingView aliPayLoadingView;

    public AliPayHeaderView(@NonNull Context context) {
        this(context,null);
    }

    public AliPayHeaderView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context){
        LayoutInflater.from(context).inflate(R.layout.header_ali_pay,this);
        aliPayLoadingView = findViewById(R.id.loading_ali_pay);
        aliPayLoadingView.setLoadingColor(Color.parseColor("#999999"),Color.parseColor("#7982b5"));
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public void onPullingDown(float fraction, float maxHeadHeight, float headHeight) {
        if (fraction<1){
            aliPayLoadingView.updateProgress(Math.round(fraction * aliPayLoadingView.getMaxProgress()));
        }else {
            aliPayLoadingView.updateProgress(aliPayLoadingView.getMaxProgress());
        }
    }

    @Override
    public void onPullReleasing(float fraction, float maxHeadHeight, float headHeight) {
        if (fraction<1){
            aliPayLoadingView.updateProgress(Math.round(fraction* aliPayLoadingView.getMaxProgress()));
        }
    }

    @Override
    public void startAnim(float maxHeadHeight, float headHeight) {
        aliPayLoadingView.startAnim();
    }

    @Override
    public void onFinish(OnAnimEndListener animEndListener) {
        animEndListener.onAnimEnd();
    }

    @Override
    public void reset() {
        aliPayLoadingView.stopAnim();
    }
}
